package fitness;

import java.io.Serializable;
import java.util.Random;

import bsh.EvalError;
import bsh.Interpreter;

import optimizers.commons.GAConstants;
import optimizers.ganeat.math.ConstantsMath;
import optimizers.ganeat.sin.ConstantsSin;
import util.MyUtil;
import model.Constants;
import model.Input;
import model.OligoSystemGeneral;

public class PulseFitness extends Fitness implements Serializable {

	@Override
	public FitnessResult evaluateFitness(OligoSystemGeneral oligoModel) {
		PulseResult result = new PulseResult();
		result.height = 0;
		result.width = 10000;
		try {
			int output = oligoModel.nameToInt.get("a");
			double[][] data = oligoModel.getTimeSeriesComplexModelTillStable();
			int firstPeak = MyUtil.getFirstPeak(data[output], 0);
			if (firstPeak == -1) {
				result.height = 0;
			}
			int firstStableTime = data[output].length - 1;
			while (data[output][firstStableTime] == 0) {
				firstStableTime--;
			}
			if (firstStableTime >= Constants.maxTime - 1) {
				result.width = 10000;
			} else {
				result.height = data[output][firstPeak]
						- data[output][firstStableTime];
				for (int j = firstPeak; j < firstStableTime; j++) {
					if (data[output][j] < data[output][firstPeak]
							- result.height / 2) {
						result.width = (j - firstPeak) * 2;
						return result;
					}
				}
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	@Override
	public FitnessResult evaluateFitnessSimple(OligoSystemGeneral indiv) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FitnessResult minFitness(OligoSystemGeneral indiv) {
		PulseResult result = new PulseResult();
		result.height = 0;
		result.width = 100;
		return result;
	}
}
